{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# Multi-Qubit Systems Workbook\n",
    "\n",
    "**What is this workbook?**\n",
    "A workbook is a collection of problems, accompanied by solutions to them. \n",
    "The explanations focus on the logical steps required to solve a problem; they illustrate the concepts that need to be applied to come up with a solution to the problem, explaining the mathematical steps required. \n",
    "\n",
    "Note that a workbook should not be the primary source of knowledge on the subject matter; it assumes that you've already read a tutorial or a textbook and that you are now seeking to improve your problem-solving skills. You should attempt solving the tasks of the respective kata first, and turn to the workbook only if stuck. While a textbook emphasizes knowledge acquisition, a workbook emphasizes skill acquisition.\n",
    "\n",
    "This workbook describes the solutions to the problems offered in the [Multi-Qubit Systems tutorial](./MultiQubitSystems.ipynb). \n",
    "\n",
    "\n",
    "**What you should know for this workbook**\n",
    "\n",
    "1. Basic single-qubit gates.\n",
    "2. The concept of tensor product."
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "For an overview of all the gates have a look at the Quickref [here](../../quickref/qsharp-quick-reference.pdf)."
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### <span style=\"color:blue\">Exercise 1</span>: Show that the state is separable\n",
    "\n",
    "$$\\frac{1}{2} \\begin{bmatrix} 1 \\\\ i \\\\ -i \\\\ 1 \\end{bmatrix} =\n",
    "\\begin{bmatrix} ? \\\\ ? \\end{bmatrix} \\otimes \\begin{bmatrix} ? \\\\ ? \\end{bmatrix}$$\n",
    "\n",
    "### Solution\n",
    "\n",
    "To separate the state into a tensor product of two single-qubit states, we need to represent it in the following way:\n",
    "\n",
    "$$\\begin{bmatrix} \\alpha \\color{red}\\gamma \\\\ \\alpha \\color{red}\\delta \\\\ \\beta \\color{red}\\gamma \\\\ \\beta \\color{red}\\delta \\end{bmatrix} = \\begin{bmatrix} \\alpha \\\\ \\beta \\end{bmatrix} \\otimes \\begin{bmatrix} \\color{red}\\gamma \\\\ \\color{red}\\delta \\end{bmatrix}$$\n",
    "\n",
    "This brings us to a system of equations:\n",
    "\n",
    "$$\\begin{cases}\n",
    "\\alpha\\gamma = \\frac{1}{2} \\\\\n",
    "\\alpha\\delta = \\frac{i}{2} \\\\\n",
    "\\beta \\gamma = \\frac{-i}{2} \\\\\n",
    "\\beta \\delta = \\frac{1}{2} \\\\\n",
    "\\end{cases}$$\n",
    "\n",
    "Solving this system of equations gives us the answer:\n",
    "\n",
    "$$\\alpha = \\frac{1}{\\sqrt2}, \\beta = \\frac{-i}{\\sqrt2}, \\gamma = \\frac{1}{\\sqrt2}, \\delta = \\frac{i}{\\sqrt2}$$\n",
    "\n",
    "$$\\frac{1}{2} \\begin{bmatrix} 1 \\\\ i \\\\ -i \\\\ 1 \\end{bmatrix} = \\frac{1}{\\sqrt2}\n",
    "\\begin{bmatrix} 1 \\\\ -i \\end{bmatrix} \\otimes \\frac{1}{\\sqrt2} \\begin{bmatrix} 1 \\\\ i \\end{bmatrix}$$"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "[Return to task 1 of the Multi-Qubit Systems tutorial.](./MultiQubitSystems.ipynb#Exercise-1:-Show-that-the-state-is-separable)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### <span style=\"color:blue\">Exercise 2</span>: Is this state separable?\n",
    "\n",
    "$$\\frac{1}{\\sqrt{2}}\\begin{bmatrix} 1 \\\\ 0 \\\\ 0 \\\\ 1 \\end{bmatrix}$$\n",
    "\n",
    "### Solution\n",
    "\n",
    "Let's assume that this state is separable and write down the system of equations to determine the coefficients of individual qubit states in the tensor product, similar to what we did in the previous exercise:\n",
    "\n",
    "$$\\begin{cases}\n",
    "\\alpha\\gamma = \\frac{1}{\\sqrt2} \\\\\n",
    "\\alpha\\delta = 0 \\\\\n",
    "\\beta \\gamma = 0 \\\\\n",
    "\\beta \\delta = \\frac{1}{\\sqrt2} \\\\\n",
    "\\end{cases}$$\n",
    "\n",
    "Now let's multiply the first and the last equations, and the second and the third equations:\n",
    "\n",
    "$$\\begin{cases}\n",
    "\\alpha\\beta\\gamma\\delta = \\frac{1}{2} \\\\\n",
    "\\alpha\\beta\\gamma\\delta = 0\n",
    "\\end{cases}$$\n",
    "\n",
    "We can see that this system of equations doesn't have a solution, which means that this state is **not separable**."
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "[Return to task 2 of the Multi-Qubit Systems tutorial.](./MultiQubitSystems.ipynb#Exercise-2:-Is-this-state-separable?)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### <span style=\"color:blue\">Exercise 3</span>: Prepare a basis state\n",
    "\n",
    "**Input:** A two-qubit system in the basis state $|00\\rangle = \\begin{bmatrix} 1 \\\\ 0 \\\\ 0 \\\\ 0 \\end{bmatrix}$.\n",
    "\n",
    "**Goal:** Transform the system into the basis state $|11\\rangle = \\begin{bmatrix} 0 \\\\ 0 \\\\ 0 \\\\ 1 \\end{bmatrix}$."
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### Solution\n",
    "\n",
    "The starting state can be represented as follows:\n",
    "$$ \\begin{bmatrix} 1 \\\\ 0 \\\\ 0 \\\\ 0 \\end{bmatrix} = |0\\rangle \\otimes |0\\rangle $$\n",
    "\n",
    "The goal state can be represented as follows:\n",
    "$$ \\begin{bmatrix} 0 \\\\ 0 \\\\ 0 \\\\ 1 \\end{bmatrix} = |1\\rangle \\otimes |1\\rangle $$\n",
    "\n",
    "Applying an **X** gate to a qubit in the $|0\\rangle$ state transforms the qubit state into the $|1\\rangle$ state. So, if we apply the **X** gate on the first qubit and the second qubit, we get the desired state."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "%kata T1_PrepareState1\n",
    "\n",
    "operation PrepareState1 (qs : Qubit[]) : Unit is Adj+Ctl {\n",
    "    X(qs[0]);\n",
    "    X(qs[1]);\n",
    "}"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "[Return to task 3 of the Multi-Qubit Systems tutorial.](./MultiQubitSystems.ipynb#Exercise-3:-Prepare-a-basis-state)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### <span style=\"color:blue\">Exercise 4</span>: Prepare a superposition of two basis states\n",
    "\n",
    "**Input:** A two-qubit system in the basis state $|00\\rangle = \\begin{bmatrix} 1 \\\\ 0 \\\\ 0 \\\\ 0 \\end{bmatrix}$.\n",
    "\n",
    "**Goal:** Transform the system into the state $\\frac{1}{\\sqrt2}\\big(|00\\rangle - |01\\rangle\\big) = \\frac{1}{\\sqrt2}\\begin{bmatrix} 1 \\\\ -1 \\\\ 0 \\\\ 0 \\end{bmatrix}$."
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### Solution\n",
    "\n",
    "We begin in the same state as the previous excercise:\n",
    "$$ \\begin{bmatrix} 1 \\\\ 0 \\\\ 0 \\\\ 0 \\end{bmatrix} = \\begin{bmatrix} 1 \\\\ 0 \\end{bmatrix} \\otimes \\begin{bmatrix} 1 \\\\ 0 \\end{bmatrix} = |0\\rangle \\otimes |0\\rangle$$\n",
    "\n",
    "The goal state can be separated as follows:\n",
    "$$ \\frac{1}{\\sqrt2} \\begin{bmatrix} 1 \\\\ -1 \\\\ 0 \\\\ 0 \\end{bmatrix} = \\begin{bmatrix} 1 \\\\ 0 \\end{bmatrix} \\otimes \\frac{1}{\\sqrt2}\\begin{bmatrix} 1 \\\\ -1 \\end{bmatrix} = |0\\rangle \\otimes \\frac{1}{\\sqrt2}\\big(|0\\rangle - |1\\rangle\\big)$$\n",
    "\n",
    "This means that the first qubit is already in the state we want it to be, but the second qubit needs to be transformed from the $ \\begin{bmatrix} 1 \\\\ 0 \\end{bmatrix} $ into $ \\frac{1}{\\sqrt{2}}\\begin{bmatrix} 1 \\\\ -1\\end{bmatrix}$ state.\n",
    "\n",
    "First, we apply the **X** gate to the second qubit; this performs the following transformation:\n",
    "$$ X |0\\rangle = \\begin{bmatrix}0 & 1 \\\\ 1 & 0 \\end{bmatrix} \\cdot \\begin{bmatrix}1 \\\\ 0 \\end{bmatrix} = \\begin{bmatrix} 0 \\\\ 1 \\end{bmatrix} = |1\\rangle  $$\n",
    "\n",
    "Second, we apply the **H** gate to the second qubit; this transforms its state into the desired one:\n",
    "$$ H|1\\rangle = \\frac{1}{\\sqrt2}\\begin{bmatrix} 1 & 1 \\\\ 1 & -1 \\end{bmatrix} \\cdot \\begin{bmatrix} 0 \\\\ 1 \\end{bmatrix} = \\frac{1}{\\sqrt2}\\begin{bmatrix} 1 \\\\ -1 \\end{bmatrix}$$"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "%kata T2_PrepareState2\n",
    "\n",
    "operation PrepareState2 (qs : Qubit[]) : Unit is Adj+Ctl {\n",
    "    X(qs[1]);\n",
    "    H(qs[1]);\n",
    "}"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "[Return to task 4 of the Multi-Qubit Systems tutorial.](./MultiQubitSystems.ipynb#Exercise-4:-Prepare-a-superposition-of-two-basis-states)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### <span style=\"color:blue\">Exercise 5</span>: Prepare a superposition with real amplitudes\n",
    "\n",
    "**Input:** A two-qubit system in the basis state $|00\\rangle = \\begin{bmatrix} 1 \\\\ 0 \\\\ 0 \\\\ 0 \\end{bmatrix}$.\n",
    "\n",
    "**Goal:** Transform the system into the state $\\frac{1}{2}\\big(|00\\rangle - |01\\rangle + |10\\rangle - |11\\rangle\\big) = \\frac{1}{2}\\begin{bmatrix} 1 \\\\ -1 \\\\ 1 \\\\ -1 \\end{bmatrix}$."
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### Solution\n",
    "\n",
    "Again, to start we will represent the goal state as a tensor product of single-qubit states; this gives us the following representation:\n",
    "\n",
    "$$ \\frac{1}{2}\\big(|00\\rangle - |01\\rangle + |10\\rangle - |11\\rangle\\big) = \\frac{1}{2}\\begin{bmatrix} 1 \\\\ -1 \\\\ 1 \\\\ -1 \\end{bmatrix} = \\frac{1}{\\sqrt2} \\begin{bmatrix} 1 \\\\ 1 \\end{bmatrix} \\otimes \\frac{1}{\\sqrt2}\\begin{bmatrix} 1 \\\\ -1 \\end{bmatrix} = \\frac{1}{\\sqrt2}\\big(|0\\rangle + |1\\rangle\\big) \\otimes \\frac{1}{\\sqrt2}\\big(|0\\rangle - |1\\rangle\\big)  $$\n",
    "\n",
    "This time we need to transform both the first and the second qubits. Let's start with the first qubit. Applying the **H** gate transforms its state as follows:\n",
    "\n",
    "$$ H|0\\rangle = \\frac{1}{\\sqrt2}\\begin{bmatrix} 1 & 1 \\\\ 1 & -1 \\end{bmatrix} \\cdot \\begin{bmatrix} 1 \\\\ 0 \\end{bmatrix} = \\frac{1}{\\sqrt2} \\begin{bmatrix} 1 \\\\ 1 \\end{bmatrix} = \\frac{1}{\\sqrt2}\\big(|0\\rangle + |1\\rangle\\big)$$\n",
    "\n",
    "For the second qubit we can use the same transformation we've seen in [exercise 4](#Exercise-4:-Prepare-a-superposition-of-two-basis-states); this will give the desired end state."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "%kata T3_PrepareState3\n",
    "\n",
    "operation PrepareState3 (qs : Qubit[]) : Unit is Adj+Ctl {\n",
    "    H(qs[0]);\n",
    "    X(qs[1]);\n",
    "    H(qs[1]);\n",
    "}"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "[Return to task 5 of the Multi-Qubit Systems tutorial.](./MultiQubitSystems.ipynb#Exercise-5:-Prepare-a-superposition-with-real-amplitudes)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### <span style=\"color:blue\">Exercise 6</span>: Prepare a superposition with complex amplitudes\n",
    "\n",
    "**Input:** A two-qubit system in the basis state $|00\\rangle = \\begin{bmatrix} 1 \\\\ 0 \\\\ 0 \\\\ 0 \\end{bmatrix}$.\n",
    "\n",
    "**Goal:** Transform the system into the state $\\frac{1}{2}\\big(|00\\rangle + e^{i\\pi/4}|01\\rangle + e^{i\\pi/2}|10\\rangle + e^{3i\\pi/4}|11\\rangle\\big) = \\frac{1}{2}\\begin{bmatrix} 1 \\\\ e^{i\\pi/4} \\\\ e^{i\\pi/2} \\\\ e^{3i\\pi/4} \\end{bmatrix}$."
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### Solution\n",
    "\n",
    "The start state is the same as the previous exercises:\n",
    "$$ \\begin{bmatrix} 1 \\\\ 0 \\\\ 0 \\\\ 0 \\end{bmatrix} = \\begin{bmatrix} 1 \\\\ 0 \\end{bmatrix} \\otimes \\begin{bmatrix} 1 \\\\ 0 \\end{bmatrix} = |0\\rangle \\otimes |0\\rangle $$\n",
    "\n",
    "The goal state, factored as a tensor product, looks like this (remember that $e^{3i\\pi/4} = e^{i\\pi/4} e^{i\\pi/2}$):\n",
    "\n",
    "$$ \\frac{1}{2}\\begin{bmatrix} 1 \\\\ e^{i\\pi/4} \\\\ e^{i\\pi/2} \\\\ e^{3i\\pi/4} \\end{bmatrix} \n",
    "= \\frac{1}{\\sqrt2} \\begin{bmatrix} 1 \\\\ e^{i\\pi/2} \\end{bmatrix} \\otimes \\frac{1}{\\sqrt2}\\begin{bmatrix} 1 \\\\ e^{i\\pi/4} \\end{bmatrix} \n",
    "= \\frac{1}{\\sqrt2}\\big(|0\\rangle + e^{i\\pi/2}|1\\rangle\\big) \\otimes \\frac{1}{\\sqrt2}\\big(|0\\rangle + e^{i\\pi/4}|1\\rangle\\big) $$\n",
    "\n",
    "We will again need to adjust the states of both qubits independently.\n",
    "\n",
    "For the first qubit, we'll start by applying the **H** gate, getting the state $\\frac{1}{\\sqrt2} \\begin{bmatrix} 1 \\\\ 1 \\end{bmatrix}$, as we've seen in the previous task. Afterwards we'll apply the **S** gate with the following result:\n",
    "\n",
    "$$ \\begin{bmatrix} 1 & 0 \\\\ 0 & i \\end{bmatrix} \\cdot \\frac{1}{\\sqrt2} \\begin{bmatrix} 1 \\\\ 1 \\end{bmatrix} = \\frac{1}{\\sqrt2} \\begin{bmatrix} 1 \\\\ i \\end{bmatrix}$$\n",
    "\n",
    "If we recall that $i = e^{i\\pi/2}$, we can write the final state of the second qubit as:\n",
    "$$ \\frac{1}{\\sqrt2} \\begin{bmatrix} 1 \\\\ e^{i\\pi/2} \\end{bmatrix} $$\n",
    "\n",
    "For the second qubit. we'll apply the **H** gate, followed by the **T** gate, with the following result:\n",
    "$$ \\begin{bmatrix} 1 & 0 \\\\ 0 & e^{i\\pi/4} \\end{bmatrix} \\cdot \\frac{1}{\\sqrt2} \\begin{bmatrix} 1 \\\\ 1 \\end{bmatrix} = \\frac{1}{\\sqrt2} \\begin{bmatrix} 1 \\\\ e^{i\\pi/4} \\end{bmatrix} $$"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "%kata T4_PrepareState4\n",
    "\n",
    "operation PrepareState4 (qs : Qubit[]) : Unit is Adj+Ctl {\n",
    "    H(qs[0]);\n",
    "    S(qs[0]);\n",
    "    H(qs[1]);\n",
    "    T(qs[1]);\n",
    "}"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "[Return to task 6 of the Multi-Qubit Systems tutorial.](./MultiQubitSystems.ipynb#Exercise-6:-Prepare-a-superposition-with-complex-amplitudes)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## Conclusion\n",
    "\n",
    "As you've seen in the exercises, you can prepare separable multi-qubit states using only single-qubit gates. \n",
    "However, to prepare and manipulate entangled states you'll need more powerful tools. \n",
    "In the [next tutorial](../MultiQubitGates/MultiQubitGates.ipynb) you will learn about multi-qubit gates which give you access to all states of multi-qubit systems."
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Q#",
   "language": "qsharp",
   "name": "iqsharp"
  },
  "language_info": {
   "file_extension": ".qs",
   "mimetype": "text/x-qsharp",
   "name": "qsharp",
   "version": "0.10"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
